package com.ruoyi.management_controller.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.management_controller.userinfo.domain.Userinfo;
import com.ruoyi.management_controller.userinfo.service.IUserinfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.utils.MD5Util.getMD5String;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2024-07-12
 */
@RestController
@RequestMapping("/userinfo/userinfo")
public class UserinfoController extends BaseController
{
    @Autowired
    private IUserinfoService userinfoService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('userinfo:userinfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(Userinfo userinfo)
    {
        startPage();
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        return getDataTable(list);
    }

    @GetMapping("/findAll")
    public AjaxResult findAll(Userinfo userinfo)
    {
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        return AjaxResult.success(list);
    }


    @GetMapping("/findByName")
    public List<Userinfo> findByName(Userinfo userinfo)
    {
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        return list;
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('userinfo:userinfo:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Userinfo userinfo)
    {
        List<Userinfo> list = userinfoService.selectUserinfoList(userinfo);
        ExcelUtil<Userinfo> util = new ExcelUtil<Userinfo>(Userinfo.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('userinfo:userinfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userinfoService.selectUserinfoById(id));
    }




    /**
     * 新增用户信息
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Userinfo userinfo)
    {
        return toAjax(userinfoService.insertUserinfo(userinfo));
    }

    
    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('userinfo:userinfo:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Userinfo userinfo)
    {
        return toAjax(userinfoService.updateUserinfo(userinfo));
    }

    /**
     * 重置密码
     */
    //@Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @RequestMapping(value = "/updatePwd", method = RequestMethod.POST)
    public AjaxResult updatePwd(@RequestBody Userinfo userinfo)
    {
        if (StringUtils.isNotEmpty(userinfo.getPhone())|| StringUtils.isNotEmpty(userinfo.getUsername())){
            Userinfo userinfo1=userinfoService.findByPhoneAndUsername(userinfo.getPhone(),userinfo.getUsername());
            if (null!=userinfo1){
                userinfo.setRawPassword(userinfo.getPassword());
                userinfo.setPassword(getMD5String(userinfo.getPassword()));
                return toAjax(userinfoService.updatePwd(userinfo));
            }else {
                return error("用户'" + userinfo.getUsername() + "'密码更新失败，登录账号和手机号不匹配");
            }
        }else {
            return AjaxResult.error("账号或手机号不能为空");
        }

    }

    @RequestMapping(value = "/restPwd", method = RequestMethod.POST)
    public AjaxResult restPwd(@RequestBody Userinfo userinfo)
    {
        if (StringUtils.isNotEmpty(userinfo.getPhone())|| StringUtils.isNotEmpty(userinfo.getUsername())){
            Userinfo userinfo1=userinfoService.findByPhoneAndUsername(userinfo.getPhone(),userinfo.getUsername());
            if (null!=userinfo1){
                userinfo.setRawPassword(userinfo.getPassword());
                userinfo.setPassword(getMD5String(userinfo.getPassword()));
                return toAjax(userinfoService.updatePwd(userinfo));
            }else {
                return error("用户'" + userinfo.getUsername() + "'密码更新失败，登录账号和手机号不匹配");
            }
        }else {
            return AjaxResult.error("账号或手机号不能为空");
        }

    }

    //@Log(title = "用户登录", businessType = BusinessType.LOGIN)
    @RequestMapping(value = "/userLogin", method = RequestMethod.POST)
    public AjaxResult userLogin(@RequestBody Userinfo userinfo,HttpServletResponse response) {
        return userinfoService.login(response,userinfo.getUsername(), userinfo.getPassword());

    }




    //@Log(title = "查询用户信息", businessType = BusinessType.SELECT)
    @GetMapping(value = "/getInfoByPhone")
    public AjaxResult getInfoByPhone(@RequestParam("phone") String phone)
    {
        return success(userinfoService.findByPhoneOrUsername(phone,phone));
    }


    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('userinfo:userinfo:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userinfoService.deleteUserinfoByIds(ids));
    }
}
